package com.sllp.C71S3TsllpShop.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sllp.C71S3TsllpShop.bean.BigType;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.Car;
import com.sllp.C71S3TsllpShop.bean.Favorite;
import com.sllp.C71S3TsllpShop.bean.Product;
import com.sllp.C71S3TsllpShop.bean.ProductExample;
import com.sllp.C71S3TsllpShop.bean.ProductExample.Criteria;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.BusinessBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.CarBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.FavoriteBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.ProductBizImpl;
import com.sllp.C71S3TsllpShop.dao.ProductMapper;

@Controller
@SessionAttributes("page")
public class BrandListAction {
	@Resource
	private ProductBizImpl pbiz;
	@Resource
	private BusinessBizImpl bbi;
	@Resource
	private CarBizImpl cbiz;
	@Resource
	private ProductMapper pm;
	@Resource 
	private FavoriteBizImpl fbi;
	
//  店铺页面
	@GetMapping({"brandlist.do","brandlist"})
	public String toBrandList(HttpSession session,@SessionAttribute("btlist") List<List<BigType>> lists,
			String pre,String next,int business, Model m,
			@SessionAttribute("loginUser") User user,
			@SessionAttribute(name="page",required=false) Integer page) {
		m.addAttribute("btlist", lists);
//		next 为空 表示时从地址栏发起的请求，否则时无限加载插件发起的请求
		if(next == null) {
			page =1;
		}else if(Integer.valueOf(next)>0) {
			page = Integer.valueOf(next);
		}
		if(pre !=null) {
			page = 1;
		}
		
		PageHelper.startPage(page,2);
		//c从新设置分页的页码，相当于page++
		m.addAttribute("page",page+1);
		m.addAttribute("pag",page-1);
		List<Product> sb = pbiz.selectByBusiness(business);
		List<Favorite> favoriteList = fbi.findfavorite(user);
		for (Product product : sb) {
			for (int i=0; i<favoriteList.size();i++) {
				if(product.getId()==favoriteList.get(i).getProduct() && favoriteList.get(i).getState()==2) { 
					product.setIsFav(1);
					break;
				}else if(product.getId()==favoriteList.get(i).getProduct() && favoriteList.get(i).getState()==1) {
					product.setIsFav(0);
					break;
				} else if(product.getId()!=favoriteList.get(i).getProduct() && i==favoriteList.size()-1){
					product.setIsFav(0);
				}
			}
		}
		m.addAttribute("productlist", sb);
		List<Business> selectByPrimaryKey = bbi.selectByPrimaryKey(business);
		m.addAttribute("business", selectByPrimaryKey);
		Long i = pbiz.countByExample(business);
		m.addAttribute("number", i);
		System.out.println(sb);
		//店铺热卖
		List<Product> byBuyNum = pm.selectByBuyNum(business);
		
		List<Product> list = new ArrayList<>();
		if(byBuyNum.size()>=3) {
			for(int j=0;j<3;j++) {
				list.add(byBuyNum.get(j));
			}
		}else {
			list.addAll(byBuyNum);
		}
		System.out.println(list);
		m.addAttribute("sbblist",list);
		return "Brandlist";
	}
	
	@ModelAttribute
	public void searchCar(@SessionAttribute("loginUser") User user,Model m) {
		if(user != null) {
			List<Car> clist=cbiz.findCar(user);
			m.addAttribute("clist", clist);
		}
	}
}
